/**
 * Project Name:fire-industry-DM-center
 * File Name:RedisServiceImpl.java
 * Package Name:com.firestone.service.base.impl
 * Date:2018年3月13日下午4:38:59
 *
*/

package com.firestone.service.base.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Service;

import com.firestone.service.base.IRedisService;

/**
 * @Description: TODO(REDIS工具封装)
 * @author CAIYJ
 * @date 2018年3月13日 下午4:38:59
 */
@Service
public class RedisServiceImpl implements IRedisService{
    @Autowired
    private RedisTemplate<?, ?> redisTemplate;



    @Override
    public long lrem(String key, long count, String value) {
       return redisTemplate.execute((RedisConnection connection) -> {
           return connection.lRem(key.getBytes(),count,value.getBytes());
        });
    }


    @Override
    public List<?> lrange(String key, long start, long end) {
        return redisTemplate.execute((RedisConnection connection) -> {
            return connection.lRange(key.getBytes(),start,end);
        });
    }

    @Override
    public void del(byte[] key) {
        redisTemplate.execute((RedisConnection connection) -> {
            connection.del(key);
            return true;
        });
    }


    @Override
    public void del(String key) {
        del(key.getBytes());
    }

    @Override
    public void set(byte[] key, byte[] value, int liveTime) {
        redisTemplate.execute((RedisConnection connection) -> {
            connection.set(key, value);
            connection.expire(key, liveTime);
            return true;
        });
    }


    @Override
    public void set(String key, String value, int liveTime) {
        set(key.getBytes(),value.getBytes(),liveTime);
    }


    @Override
    public void set(String key, String value) {
        redisTemplate.execute((RedisConnection connection) -> {
            RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
            connection.set(serializer.serialize(key), serializer.serialize(value));
            return true;
        });
    }


    @Override
    public void set(byte[] key, byte[] value) {
        redisTemplate.execute((RedisConnection connection) -> {
            connection.set(key, value);
            return true;
        });
    }


    @Override
    public String get(String key) {
        return redisTemplate.execute((RedisConnection connection) -> {
            RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
            byte[] value = connection.get(serializer.serialize(key));
            return serializer.deserialize(value);
        });
    }


    @Override
    public byte[] get(byte[] key) {
        return redisTemplate.execute((RedisConnection connection) -> {
            return connection.get(key);
        });
    }



    @Override
    public boolean exists(String key) {
       return exists(key.getBytes());
    }

    @Override
    public boolean exists(byte[] key) {
        return redisTemplate.execute((RedisConnection connection) -> {
            return connection.exists(key);
        });
    }


    @Override
    public boolean expire(String key, int seconds) {
        return redisTemplate.execute((RedisConnection connection) -> {
            return connection.expire(key.getBytes(),seconds);
        });
    }
}

